<?php
/**
 * @author wangkangchao
 */
namespace app\admin\controller\communal;

use app\common\controller\Backend;
use think\Db;
use think\Exception;
use app\admin\model\communal\FootConfig as ModelFootConfig;
use app\admin\validate\communal\FootConfig as ValidateFootConfig;

/**
 * [自建]底部配置控制器
 */
class FootConfig extends Backend
{
    protected $model = null;
    //当前登录管理员所有子组别
    protected $childrenGroupIds = [];
    //当前组别列表数据
    protected $groupdata = [];
    //无需要权限判断的方法
    protected $noNeedRight = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new ModelFootConfig();
        $this->validate = new ValidateFootConfig();
      
    }

    /**
     * 查看
     */
    public function index()
    {
        $info_id = db('foot_config')->value('id');
        $info_id ? $ids = $info_id : $ids = input('post.id');
        $row = $this->model->get($ids);
        if(!$row){
            if ($this->request->isPost()) {
                $params = $this->request->post("row/a");
                if ($params) {
                    $params = $this->preExcludeFields($params);
                    $params['weigh'] = getMillisecond();
                    $result = false;
                    Db::startTrans();
                    try {
                        //是否采用模型验证
                        if ($this->modelValidate) {
                            $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                            $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                            $this->model->validateFailException(true)->validate($validate);
                        }
                        $result = $this->model->allowField(true)->save($params);
                        Db::commit();
                    } catch (ValidateException $e) {
                        Db::rollback();
                        $this->error($e->getMessage());
                    } catch (PDOException $e) {
                        Db::rollback();
                        $this->error($e->getMessage());
                    } catch (Exception $e) {
                        Db::rollback();
                        $this->error($e->getMessage());
                    }
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error(__('No rows were inserted'));
                    }
                }
                $this->error(__('Parameter %s can not be empty', ''));
            }
        }else{
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                if (!in_array($row[$this->dataLimitField], $adminIds)) {
                    $this->error(__('You have no permission'));
                }
            }

            if ($this->request->isPost()) {
                $params = $this->request->post("row/a");
                if ($params) {

                    $params = $this->preExcludeFields($params);
                    $params['weigh'] = getMillisecond();
                    $result = false;
                    Db::startTrans();
                    try {
                        //是否采用模型验证
                        if ($this->modelValidate) {
                            $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                            $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                            $row->validateFailException(true)->validate($validate);
                        }

                        $result = $row->allowField(true)->save($params);
                        Db::commit();
                    } catch (ValidateException $e) {
                        Db::rollback();
                        $this->error($e->getMessage());
                    } catch (PDOException $e) {
                        Db::rollback();
                        $this->error($e->getMessage());
                    } catch (Exception $e) {
                        Db::rollback();
                        $this->error($e->getMessage());
                    }
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error(__('No rows were updated'));
                    }
                }
                $this->error(__('Parameter %s can not be empty', ''));
            }
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


}

    

    

